package com.kgcx.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;

@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "kgcx.swagger", name = "enable", havingValue = "true")
public class SwaggerConfig {
    @Value("${eureka.client.serviceUrl.defaultZone:www.baidu.com}")
    private String url;
    @Value("${spring.application.name:kgcx}")
    private String serviceName;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //Timestamp的字段示例用Long表示
                .directModelSubstitute(Timestamp.class, Long.class)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.kgcx"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(serviceName)
                .description("空港出行" + serviceName + "服务接口文档")
                .termsOfServiceUrl(url)
                .version("1.0")
                .build();
    }

//    @Bean
//    public JacksonModuleRegistrar swagger2Module() {
//        return new Swagger2JacksonModule() {
//            @Override
//            public void maybeRegisterModule(ObjectMapper objectMapper) {
//                super.maybeRegisterModule(objectMapper);
//                if (objectMapper.findMixInClassFor(Swagger.class) == null) {
//                    objectMapper.setDateFormat(new SimpleDateFormat(Cons.DATE_FORMAT_YYYYMMDDHHMMSSSSS));
//                }
//
//            }
//        };
//    }
}